package co.fitstart.mobile.web.controller.credit.trading;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.entity.credit.order.CreditOrder;
import co.fitstart.entity.user.User;
import co.fitstart.mobile.service.CreditOrderService;
import co.fitstart.mobile.service.CreditTradingService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/credit-trading")
public class CreditTradingController {
    
    private static final Logger logger = LoggerFactory.getLogger(CreditTradingController.class);

    @Autowired
    private CreditOrderService creditOrderService;
    
    @Autowired
    private CreditTradingService creditTradingService;
    
    /**
     * 
     * @param productId
     * @return
     */
    @WechatAuthority
    @MobileDetect
    @Router(targetUrl = "/required-wechat")
    @RequestMapping(value = "/exchange", method = RequestMethod.POST)
    public @ResponseBody String doExchange(@RequestParam("creditProductId") Long creditProductId, 
                                           @RequestParam("userAddressId") Long userAddressId,
                                           @RequestParam("amount") Integer amount,
                                           @Secure User user) {
        
        Result result = new Result();

        try {
            
            int creditOrderId = creditTradingService.doExchange(userAddressId, creditProductId, amount, user);
            
            result.success().data("creditOrderId", creditOrderId);
            
        } catch (Exception e) {
            
            result.fail().message(e.getMessage());
            
            logger.error("CreditTradingController#doExchange() error", e);
        }
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param orderId
     * @return
     */
    @WechatAuthority
    @MobileDetect
    @Router(targetUrl = "/required-wechat")
    @RequestMapping(value = "/result", params = "orderId", method = RequestMethod.GET)
    public ModelAndView result(@RequestParam("orderId") Long orderId) {
        
        ModelAndView modelAndView = new ModelAndView("credit/trading/credit-trading-result");
        
        CreditOrder creditOrder = creditOrderService.load(orderId);
        
        modelAndView.addObject("creditOrder", creditOrder);
        
        return modelAndView;
    }
}
